package com.yoso.shop.infrastructure.factory;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.yoso.base.infrastructure.utils.IdUtil;
import com.yoso.shop.application.service.ICommodityService;
import com.yoso.shop.application.service.IConsumerService;
import com.yoso.shop.infrastructure.constants.user.UserConstants;
import com.yoso.shop.interfaces.vo.cart.CartVo;
import com.yoso.shop.interfaces.vo.commodity.CommodityVo;
import com.yoso.shop.interfaces.vo.order.OrderVo;
import com.yoso.shop.interfaces.vo.stock.StockVo;
import com.yoso.shop.interfaces.vo.user.ConsumerVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author shizejin
 * @date 2022/1/29
 * @description:
 */

@Component
public class ShopBeanFactory {
    @Autowired
    private IConsumerService consumerService;
    @Autowired
    private ICommodityService commodityService;

    /**
     * 购物项->订单
     */
    public OrderVo buildOrderVo(@NotNull CartVo cartVo) {

        //根据id到用户表找全信息
        ConsumerVo user = consumerService.findByUsername(cartVo.getBuyerId());
//        根据商品代码查询商品分支信息
        CommodityVo commodity = commodityService.findByCode(cartVo.getCommodityCode());

        return OrderVo.builder()
                .code(IdUtil.buildOrderId())
                .commodityCode(cartVo.getCommodityCode())
                .commodityName(cartVo.getCommodityName())
                .count(cartVo.getCount())
                .master(commodity.getMaster())
                .branch(commodity.getBranch())
                .color(cartVo.getColor())
                .size(cartVo.getSize())
                .price(cartVo.getPrice())
                .subtotal(cartVo.getSubtotal())
                .orderTime(DateUtil.now())
                .buyerId(cartVo.getBuyerId())
                .buyerName(cartVo.getBuyerName())
                .buyerPhone(user.getPhone())
                .buyerAddr(user.getAddress())
                .build();
    }

    /**
     * 订单->商品
     */
    public static CommodityVo buildCommodityVo(@NotNull OrderVo orderVo) {
        return CommodityVo.builder()
                .code(orderVo.getCommodityCode())
                .name(orderVo.getCommodityName())
                .master(orderVo.getMaster())
                .branch(orderVo.getBranch())
                .price(orderVo.getPrice())
                .color(orderVo.getColor())
                .size(orderVo.getSize())
                .number(orderVo.getCount())
                .build();
    }

    /**
     * 订单->库存
     */
    public static StockVo buildStockVo(@NotNull OrderVo orderVo) {
        return StockVo.builder()
                .code(orderVo.getCommodityCode())
                .name(orderVo.getCommodityName())
                .color(orderVo.getColor())
                .size(orderVo.getSize())
                .master(orderVo.getMaster())
                .branch(orderVo.getBranch())
                .number(orderVo.getCount())
                .build();
    }

    /**
     * 商品->库存
     */
    public static StockVo buildStockVo(@NotNull CommodityVo commodityVo) {
        return StockVo.builder()
                .code(commodityVo.getCode())
                .name(commodityVo.getName())
                .color(commodityVo.getColor())
                .size(commodityVo.getSize())
                .master(commodityVo.getMaster())
                .branch(commodityVo.getBranch())
                .number(commodityVo.getNumber())
                .build();
    }

    /**
     * 订单->购物项
     */
    public static CartVo buildCartVo(@NotNull OrderVo orderVo) {
        return CartVo.builder()
                .commodityCode(orderVo.getCommodityCode())
                .commodityName(orderVo.getCommodityName())
                .color(orderVo.getColor())
                .size(orderVo.getSize())
                .count(orderVo.getCount())
                .buyerId(orderVo.getBuyerId())
                .buyerName(orderVo.getBuyerName())
                .subtotal(orderVo.getSubtotal())
                .price(orderVo.getPrice())
                .build();
    }


    /**
     * 初始化用户
     * 实现密码的加密
     */
    public static ConsumerVo initConsumerVo(Object data) {
        ConsumerVo consumerVo = JSON.parseObject(JSON.toJSONString(data), ConsumerVo.class);
        consumerVo.setPassword(SecureUtil.md5(consumerVo.getPassword()));
        consumerVo.setUpdateTime(DateUtil.now());
        if (ObjectUtils.isEmpty(consumerVo.getCreatTime())) {
            consumerVo.setCreatTime(DateUtil.now());
        }
        if (StringUtils.isBlank(consumerVo.getName())) {
            consumerVo.setName(consumerVo.getUsername());
        }
        if (StringUtils.isBlank(consumerVo.getIconPath())) {
            consumerVo.setIconPath(UserConstants.DEFAULT_ICON_PATH);
        }
        return consumerVo;
    }


}
